package com.ereport.controller.adminController;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ereport.dao.AdminExeBudgetDAO;
import com.ereport.dao.ExecutiveDAO;
import com.ereport.entity.AdminExeBudget;
import com.ereport.entity.Executives;

@Controller
@RequestMapping("/exeBudget")
public class ExecutiveBudgetController {
	@RequestMapping("/open.do")
	public ModelAndView open(HttpServletRequest req,HttpSession session,ModelAndView model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			model.setViewName("/jsp/login.jsp");
			return model;
		}
		ExecutiveDAO exdao=new ExecutiveDAO();
		List<Executives> exlist=null;
		try {
			exlist=exdao.getExecutives();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		model.setViewName("/jsp/admin/executiveBudget.jsp");
		model.addObject("exlist", exlist);
		return model;
	}
	@RequestMapping(value="/save.do", method=RequestMethod.POST)
	public ModelAndView insert(HttpServletRequest req,HttpSession session,ModelAndView model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			model.setViewName("/jsp/login.jsp");
			return model;
		}
		String eid=req.getParameter("exeName");
		int exeid=0;
		if(eid.length()!=0){
			exeid=Integer.parseInt(eid);
		}
		String frmYear=req.getParameter("fromYear");
		String toYear=req.getParameter("toYear");
		String fyear=frmYear+"-"+toYear;
		AdminExeBudgetDAO aebdao=new AdminExeBudgetDAO();
		try {
			aebdao.deletAdminExeBudget(ex.getId(),exeid,fyear);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		for(int i=1;i<=12;i++){
			String t=req.getParameter("te"+i);
			double te=0;
			if(t.length()!=0){
				te=Double.parseDouble(t);
			}
			String c=req.getParameter("cp"+i);
			double cp=0;
			if(c.length()!=0){
				cp=Double.parseDouble(c);
			}
			String f=req.getParameter("fe"+i);
			double fe=0;
			if(f.length()!=0){
				fe=Double.parseDouble(f);
			}
			String m=req.getParameter("mnth"+i);
			
			try {
				aebdao.insert(new AdminExeBudget(ex,exeid,fyear,te,cp,fe,m));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		model.setViewName("redirect:open.do");
		model.addObject("success", "Successfully Inserted");
		return model;
	}
	@RequestMapping(value="/getExeBudget.do", method=RequestMethod.GET)
	public @ResponseBody
	String getExeBudget(HttpServletRequest req,HttpSession session,ModelAndView model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		
		String eid=req.getParameter("exeName");
		int exeid=0;
		if(eid.length()!=0){
			exeid=Integer.parseInt(eid);
		}
		String frmYear=req.getParameter("fromYear");
		String toYear=req.getParameter("toYear");
		int fy=Integer.parseInt(frmYear);
		int ty=Integer.parseInt(toYear);
		String fyear=frmYear+"-"+toYear;
		AdminExeBudgetDAO aedao=new AdminExeBudgetDAO();
		List<AdminExeBudget> aeblist=null;
		try {
			aeblist=aedao.getExeBudget(ex.getId(),exeid,fyear);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		StringBuilder sb=new StringBuilder();
		if(aeblist!=null){
		for(int i=0;i<aeblist.size();i++){
			double te=aeblist.get(i).getTe();
			double cp=aeblist.get(i).getCp();
			double fe=aeblist.get(i).getFe();
			sb.append(te+"/"+cp+"/"+fe+"/");
		}
		return sb.toString();
		}else {
			return "";
		}
	}
}
